<?php
/**
 * 创建榜单-品牌榜
 * 更新时间：每周一，3点41
 */
ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\bigData\BrandRankingBrand as bigBrandRankingBrand;
use Biz\model\bigData\BrandRankingHot as bigBrandRankingHot;
use Biz\model\wiki\WikiRankingBrand;
use Biz\model\wiki\WikiRankingConfig;
use Biz\model\wiki\WikiRankingWeek;
use Biz\service\wikiRanking\Ranking;
use Biz\tools\Mail;

$start = time();
$dayArr = Ranking::getCreateRankingDay();
$day = $dayArr['day'];
$sourceDay = $dayArr['source_day'];
$now = date('Ymd');
if ($sourceDay != $now) {
    var_dump('当前日期为：' . $now . '不能创建：' . $sourceDay . '的榜单');
    return false;
}
$info = WikiRankingWeek::getByWhere(['ranking_day' => $day]);
if (!$info) {
    var_dump('不存在的榜单期数');
    return false;
}

$list = [];
$bigData = bigBrandRankingBrand::select('*', ['day' => $sourceDay]);
if (empty($bigData)) {
    var_dump('未查询到榜单数据');
    Mail::send(['tangyiwan@haohaozhu.com', 'caowenxue@haohaozhu.com'], '未查询到榜单数据', '未查询到榜单数据#sql:' . json_encode(bigBrandRankingBrand::log()));
    return false;
}
$brandRankingList = [];
$rankingIds = [];
foreach ($bigData as $row) {
    $brandRankingList[$row['ranking_id']][] = $row;
    $rankingIds[] = $row['ranking_id'];
}
$rankingList = WikiRankingConfig::selectByWhere(['id' => $rankingIds], ['id', 'ranking_name']);
$rankingList = array_column($rankingList, null, 'id');
foreach ($brandRankingList as $rankingId => $row) {
    $soreSort = array_column($row, 'score');
    $sortList = $row;
    array_multisort($soreSort, SORT_DESC, $sortList);
    $sortList = array_slice($sortList, 0, 100);
    $list[$rankingId] = $sortList;
}
foreach ($list as $rankingId => $rowList) {
    $data = [];
    $rankingName = isset($rankingList[$rankingId]) ? $rankingList[$rankingId]['ranking_name'] : '';
    foreach ($rowList as $row) {
        $hotWhere = [
            'day' => $sourceDay,
            'brand_id' => $row['brand_id'],
            'ranking_id' => $row['ranking_id'],
            'ORDER' => ['score' => 'DESC']
        ];
        $hotBasicId = bigBrandRankingHot::get('basic_id', $hotWhere);
        if (!empty($hotBasicId)) {
            $data[] = [
                'ranking_day' => $day,
                'ranking_id' => $rankingId,
                'ranking_name' => $rankingName,
                'brand_id' => $row['brand_id'],
                'score' => $row['score'],
                'basic_id' => $hotBasicId,
                'addtime' => date('Y-m-d H:i:S'),
                'edittime' => date('Y-m-d H:i:S'),
            ];
        }
    }
    var_dump('创建「' . $rankingName . '」' . '#' . $rankingId . '的品牌榜单');
    WikiRankingBrand::insert($data);
}

//检查是否创建数据成功
$check = WikiRankingBrand::selectCountWhere(['ranking_day' => $day]);
if (!$check) {
    throw new \Exception('未查询到创建的' . $day . '品牌榜榜单数据');
}

$end = time();
var_dump(date('Y-m-d-H-i-s') . '第' . $day . '期榜单,创建榜单-品牌榜,耗时:' . ($end - $start) . '秒，' . (($end - $start) / 60) . '分');
//22秒



